





//
//  PriceTableview.m
//  住房条件帅选器
//
//  Created by MB__Lee on 2018/5/16.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "PriceTableview.h"
#import "MBNetTool.h"
#import "PriceModel.h"
#import "PriceCell.h"
#import "PriceSearchView.h"

#define Cell_hight MBScale(50)  //cell 高度
#define LastCell_hight MBScale(67)  //最后一行的高度

@interface PriceTableview()<UITableViewDelegate,UITableViewDataSource,PriceSearchViewDelegate>

@property(nonatomic,strong)NSMutableArray *priceMut;


@property(nonatomic,strong)PriceSearchView *priceSearchView;

@end

static NSString *PriceCell_Identi = @"PriceCell";
static NSString *PriceSearchViewIdenti = @"PriceSearchView";

@implementation PriceTableview
-(NSMutableArray *)priceMut{
    if (!_priceMut) {
        _priceMut = [NSMutableArray array];
        
    }
    return _priceMut;
}
-(PriceSearchView *)priceSearchView{
    if (!_priceSearchView) {
        _priceSearchView = [[NSBundle mainBundle] loadNibNamed:PriceSearchViewIdenti owner:nil options:nil].lastObject;
        _priceSearchView.delegate = self;
   
        
    }
    return _priceSearchView;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        //1.从网络获取价格区间
//        [self priceTableview_getRentSource];
//        self.scrollEnabled = NO;
        
        [self registerNib:[UINib nibWithNibName:PriceCell_Identi bundle:nil] forCellReuseIdentifier:PriceCell_Identi];
//        self.contentInset = UIEdgeInsetsMake(0, 0, MBScaleH(50), 0);
        self.showsVerticalScrollIndicator = NO;
        self.bounces = NO;
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        //添加搜索框view
        
        
        
    }
    return self;
}

#pragma mark --tableView 数据源 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.priceMut.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PriceCell *cell = [tableView dequeueReusableCellWithIdentifier:PriceCell_Identi];
    cell.priceModel = self.priceMut[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == self.priceMut.count - 1) {
        [cell addSubview:self.priceSearchView];
        self.priceSearchView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);
    }
    
    return  cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.priceMut.count - 1) {
        return  LastCell_hight;
    }
    
    return  Cell_hight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row  == self.priceMut.count - 1) {
        return;
    }
    
    if ([self.ptDelegate respondsToSelector:@selector(priceTableview:priceModel:)]) {
        PriceModel *model = self.priceMut[indexPath.row];
        [self.ptDelegate priceTableview:self priceModel:model];
        [self removeFromSuperview];
    }
    
}

-(void)setPriceType:(NSInteger)priceType{
 
    _priceType = priceType;
    [self priceTableview_getRentSource];
}
#pragma mark --获取网络数据
-(void)priceTableview_getRentSource{
    
    NSString *category = [self getCategery];
    
    
    NSDictionary *dic =@{@"category":category,
                         @"type":@"rent",
                         @"city":MBCityCode
                         };
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [MBNetTool MBPost:Search_show paramater:dict success:^(id response) {
        if ([response[@"code"] integerValue] == 10000) {
            for (NSDictionary *priceDict in response[@"data"]) {
                PriceModel *priceModel = [[PriceModel alloc] initWithDictionary:priceDict error:nil];
                [self.priceMut addObject:priceModel];
            }
        }
        PriceModel *noModel = [[PriceModel alloc] init];
        noModel.name = @"不限";
        [self.priceMut insertObject:noModel  atIndex:0];
        
        PriceModel *placeHoloderModel = [[PriceModel alloc] init];
        placeHoloderModel.name = @" ";
        [self.priceMut addObject:placeHoloderModel];
        
        
        //计算实际cell 的高度并且赋值
        CGRect frame = self.frame;
        CGFloat caculateHight = (self.priceMut.count - 1) * Cell_hight  + LastCell_hight;
        if (caculateHight > 0.7 *SCREEN_HIGHT) {
            frame.size.height = 0.7 * SCREEN_HIGHT;
        }else{
            frame.size.height = caculateHight;
        }
        self.frame = frame;
        
        
        
        self.backgroundColor = [UIColor greenColor];
        [self reloadData];
        
        //默认选中
        [self selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
        
    } failure:^(NSError *error) {
        
    }];
}
-(NSString *)getCategery{
    NSString *category;
    if (self.priceType == PriceTableviewTypeResidence) {
        category = @"1001";
    }else if (self.priceType == PriceTableviewTypeHotel){
        category = @"1002";
    }else if (self.priceType == PriceTableviewTypeOffBuilding){
        category = @"1003";
    }else if (self.priceType == PriceTableviewTypeShop){
        category = @"1004";
    }
    return  category;
}
-(void)priceTableViewSetPlaceHolderMin:(NSString *)min max:(NSString *)max{
    [self.priceSearchView setPlaceHolderMin:min max:max];
}


#pragma mark --priceSearchView 代理方法
-(void)PriceSearchView:(PriceSearchView *)priceview sendPriceModel:(PriceModel *)priceModel{
    
    if ([self.ptDelegate respondsToSelector:@selector(priceTableview:priceModel:)]) {
        [self.ptDelegate priceTableview:self priceModel:priceModel];
     
    }
    
}



@end
